package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ysd.dao.impl.AdminDaoImpl;
import com.ysd.entity.Admin;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminServlet extends BaseServlet {

	/**
	 * localhost:8080/kouhong/adminServlet?reqName=login
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	public void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		AdminDaoImpl aDao = new AdminDaoImpl();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		Admin admin = aDao.getAdminById(username);
		
		if(admin==null) {//没查到,账号错误
			out.print(Result.toClient(false, "", "账号错误"));
		}else {//查到了账号正确
			if (admin.getPassword().equals(password)) {
				HttpSession session = request.getSession();//创建session对象
				session.setAttribute("admin", admin);//将admin存入session对象中
				out.print(Result.toClient(true, admin, "登陆成功"));
			} else {
				out.print(Result.toClient(false, "", "登陆失败"));
			}
		}

		out.flush();
		out.close();

	}

}
